import { registerAs } from "@nestjs/config";
import { config as dotenvConfig } from "dotenv";
import { DataSource, DataSourceOptions } from "typeorm";

dotenvConfig({ path: ".env" });

export const appTypeormConfig = {
    type: "postgres",
    host: `${process.env.POSTGRES_HOST}`,
    port: `${process.env.POSTGRES_PORT}`,
    username: `${process.env.POSTGRES_USER}`,
    password: `${process.env.POSTGRES_PASSWORD}`,
    database: `${process.env.POSTGRES_DB}`,
    autoLoadEntities: true,
    logging: true,
    synchronize: false
};

export const config = {
    ...appTypeormConfig,
    entities: ["src/**/dao/*.entity.{ts,js}"],
    migrations: ["src/migrations/*.{ts,js}"]
};

export default registerAs("typeorm", () => appTypeormConfig);
export const connectionSource = new DataSource(config as DataSourceOptions);
